from django.db import models

# Create your models here.
class Book(models.Model):
    title = models.CharField(max_length=32)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    pub_date = models.DateField()
    publish = models.ForeignKey("Publish", on_delete=models.CASCADE)#外键， 级联删除
    authors = models.ManyToManyField("Author")#多对多键 多个作者多本书
    def __str__(self):
        return "title={0},publish={1},price={2},pub_date={3}.authors={4}"\
            .format(self.title,self.publish.name,self.price,self.pub_date,self.authors.name)


class Publish(models.Model):
    name = models.CharField(max_length=32)
    city = models.CharField(max_length=64)
    email = models.EmailField()
    def __str__(self):
        return "name={0},city={1},email={2}".format(self.name,self.city,self.email)


class Author(models.Model):
    name = models.CharField(max_length=32)
    age = models.SmallIntegerField()
    au_detail = models.OneToOneField("AuthorDetail", on_delete=models.CASCADE)#一对一 级联
    def __str__(self):
        return "name={0},age={1},au_detail={2}".format(self.name,self.age,self.au_detail)


class AuthorDetail(models.Model):
    # 它是一个可迭代的二元组(例如，列表或是元组)，用来给字段提供选择项。
    # 如果设置了 choices ，Django 的管理后台就会显示选择框，
    # 而不是标准的文本框，而且这个选择框的选项就是 choices 中的元组。
    # 每个元组中的第一个元素，是存储在数据库中的值；
    # 第二个元素是在管理界面或 ModelChoiceField 中用作显示的内容。
    gender_choices = (
        (0, "女"),
        (1, "男"),
        (2, "保密"),
    )
    gender = models.SmallIntegerField(choices=gender_choices)
    tel = models.CharField(max_length=32)
    addr = models.CharField(max_length=64)
    birthday = models.DateField()
    def __str__(self):
        return "gender:{0},tel:{1},addr={2},birthday={3}"\
            .format(self.gender,self.tel,self.addr,self.birthday)


class Emp(models.Model):
    name = models.CharField(max_length=32)
    age = models.IntegerField()
    salary = models.DecimalField(max_digits=8, decimal_places=2)
    dep = models.CharField(max_length=32)
    province = models.CharField(max_length=32)

class Emps(models.Model):
    name = models.CharField(max_length=32)
    age = models.IntegerField()
    salary =     models.DecimalField(max_digits=8, decimal_places=2)
    dep = models.ForeignKey("Dep", on_delete=models.CASCADE)
    province = models.CharField(max_length=32)
class Dep(models.Model):
    title = models.CharField(max_length=32)